#ifndef _InfoBuffer_H_
#define _InfoBuffer_H_
#include <iostream>
#include <map>
#include <string>
#include <pthread.h>
#include "../Utils/JsonUtil.h"
using namespace std;

class InfoBuffer{
protected:
	InfoBuffer()=default;
public:
	static pthread_mutex_t mutex;
	static InfoBuffer* get_instance();
	void putEncryptKey(const string &key);
	string getEncryptKey();
	void putEncryptSetting(const string &jsonSetting);
	map<string,string> getEncryptSetting();
private:
	static InfoBuffer *buffer;
	map<string,string> buffer_map;
};
//make this object singleton
pthread_mutex_t InfoBuffer::mutex;
InfoBuffer* InfoBuffer::buffer=NULL;

InfoBuffer* InfoBuffer::get_instance()
{
	if(buffer==NULL)
	{
		pthread_mutex_lock(&mutex);
		if(buffer==NULL)
			buffer=new InfoBuffer();
		pthread_mutex_unlock(&mutex);
	}
	return buffer;
}

void InfoBuffer::putEncryptKey(const string &key)
{
	map<string,string> ::iterator iter;
	iter=buffer_map.find("encrypt_key");
	if(iter !=buffer_map.end()){
		buffer_map.erase(iter);
	}
	buffer_map.insert(make_pair("encrypt_key",key));
}
string InfoBuffer::getEncryptKey()
{
	string key=buffer_map["encrypt_key"];
	return key;
}
void InfoBuffer::putEncryptSetting(const string &jsonSetting)
{
	buffer_map.insert(make_pair("encrypt_setting",jsonSetting));
}
map<string,string> InfoBuffer::getEncryptSetting()
{
	string jsonSetting=buffer_map["encrypt_setting"];
	map<string,string> setting_map=JsonUtil::json_to_map(jsonSetting);
	return setting_map;
}
#endif